package com.supermarket.controller;

import com.supermarket.annotation.OperationLogAnnotation;
import com.supermarket.dto.ProductPageQueryDto;
import com.supermarket.entity.Product;
import com.supermarket.service.ProductService;
import com.supermarket.utils.PageResult;
import com.supermarket.utils.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/product")
@Slf4j
@Api(tags = "商品管理", description = "提供商品相关的操作接口")  // 类级别的 Swagger 注解
public class ProductController {

    @Autowired
    private ProductService productService;

    /**
     * 分页查询商品列表
     */
    @ApiOperation(value = "分页查询商品列表", notes = "根据分页条件查询商品列表")
    @GetMapping("/list")
    @OperationLogAnnotation(operation = "查询商品列表", operationType = "查询")
    public Result<PageResult> list(ProductPageQueryDto productPageQueryDto) {
        log.info("分页查询商品列表：{}", productPageQueryDto);
        PageResult pageResult = productService.list(productPageQueryDto);
        return Result.success(pageResult);
    }

    /**
     * 根据id查询商品
     */
    @ApiOperation(value = "根据id查询商品", notes = "根据商品id获取商品信息")
    @GetMapping("/{id}")
    @OperationLogAnnotation(operation = "根据id查询商品", operationType = "查询")
    public Result<Product> getById(@ApiParam(value = "商品ID", required = true) @PathVariable Long id) {
        log.info("根据id查询商品：{}", id);
        Product product = productService.getById(id);
        return Result.success(product);
    }

    /**
     * 新增商品
     */
    @ApiOperation(value = "新增商品", notes = "新增商品信息")
    @PostMapping
    @OperationLogAnnotation(operation = "新增商品", operationType = "新增")
    public Result<String> save(@ApiParam(value = "商品信息", required = true) @RequestBody Product product) {
        log.info("新增商品：{}", product);
        productService.save(product);
        return Result.success();
    }

    /**
     * 更新商品
     */
    @ApiOperation(value = "更新商品", notes = "更新商品信息")
    @PutMapping
    @OperationLogAnnotation(operation = "更新商品信息", operationType = "更新")
    public Result<String> update(@ApiParam(value = "商品信息", required = true) @RequestBody Product product) {
        log.info("更新商品：{}", product);
        productService.update(product);
        return Result.success();
    }

    /**
     * 删除商品
     */
    @ApiOperation(value = "删除商品", notes = "根据商品ID删除商品")
    @DeleteMapping("/{id}")
    @OperationLogAnnotation(operation = "删除商品", operationType = "删除")
    public Result<String> delete(@ApiParam(value = "商品ID", required = true) @PathVariable Long id) {
        log.info("删除商品：{}", id);
        productService.delete(id);
        return Result.success();
    }

    /**
     * 更新商品状态
     */
    @ApiOperation(value = "更新商品状态", notes = "修改商品的状态（例如上架、下架等）")
    @PutMapping("/status/{status}/{id}")
    @OperationLogAnnotation(operation = "修改商品状态", operationType = "更新")
    public Result<String> updateStatus(
            @ApiParam(value = "商品状态", required = true) @PathVariable Integer status,
            @ApiParam(value = "商品ID", required = true) @PathVariable Long id) {
        log.info("更新商品状态：status={}, id={}", status, id);
        productService.updateStatus(id, status);
        return Result.success();
    }
}
